package com.zackeus.mvvm.api;

import android.net.ParseException;

import com.google.gson.JsonParseException;
import com.google.gson.stream.MalformedJsonException;
import com.zackeus.mvvm.api.response.ResponseStatus;
import com.zackeus.mvvm.api.response.ResponseThrowable;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.HttpURLConnection;

import retrofit2.HttpException;


/**
 * 异常处理钩子
 */
public class ExceptionHandle {

    public static ResponseThrowable handleException(Throwable e) {
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            String message;

            switch (httpException.code()) {
                case HttpURLConnection.HTTP_UNAUTHORIZED:
                    message = ResponseStatus.HTTP_UNAUTHORIZED;
                    break;
                case HttpURLConnection.HTTP_FORBIDDEN:
                    message = ResponseStatus.HTTP_FORBIDDEN;
                    break;
                case HttpURLConnection.HTTP_NOT_FOUND:
                    message = ResponseStatus.HTTP_NOT_FOUND;
                    break;
                case HttpURLConnection.HTTP_CLIENT_TIMEOUT:
                    message = ResponseStatus.HTTP_CLIENT_TIMEOUT;
                    break;
                case HttpURLConnection.HTTP_INTERNAL_ERROR:
                    message = ResponseStatus.HTTP_SERVER_ERROR;
                    break;
                case HttpURLConnection.HTTP_UNAVAILABLE:
                    message = ResponseStatus.HTTP_UNAVAILABLE;
                    break;
                default:
                    message = "网络错误";
                    break;
            }
            return new ResponseThrowable(httpException.code(), message, e);
        }

        if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException || e instanceof MalformedJsonException) {
            return new ResponseThrowable(HttpURLConnection.HTTP_INTERNAL_ERROR, "解析错误", e);
        }

        if (e instanceof ConnectException) {
            return new ResponseThrowable(HttpURLConnection.HTTP_INTERNAL_ERROR, "连接失败", e);
        }

        if (e instanceof javax.net.ssl.SSLException) {
            return new ResponseThrowable(HttpURLConnection.HTTP_INTERNAL_ERROR, "证书验证失败", e);
        }

        if (e instanceof ConnectTimeoutException) {
            return new ResponseThrowable(HttpURLConnection.HTTP_CLIENT_TIMEOUT, "连接超时", e);
        }

        if (e instanceof java.net.SocketTimeoutException) {
            return new ResponseThrowable(HttpURLConnection.HTTP_CLIENT_TIMEOUT, "连接超时", e);
        }

        if (e instanceof java.net.UnknownHostException) {
            return new ResponseThrowable(HttpURLConnection.HTTP_CLIENT_TIMEOUT, "主机地址未知", e);
        }

        return new ResponseThrowable(HttpURLConnection.HTTP_INTERNAL_ERROR, "未知错误", e);
    }

}

